/// 聊天消息类型枚举
enum ChatMessageType {
  text, // 文本消息
  callFailed, // 拨打失败消息
}

/// 单聊消息模型
class ChatMessageModel {
  final String messageId; // 消息ID
  final String senderId; // 发送者ID
  final String receiverId; // 接收者ID
  final ChatMessageType messageType; // 消息类型
  final String content; // 消息内容
  final DateTime sendTime; // 发送时间
  final bool isRead; // 是否已读

  const ChatMessageModel({
    required this.messageId,
    required this.senderId,
    required this.receiverId,
    required this.messageType,
    required this.content,
    required this.sendTime,
    this.isRead = false,
  });

  /// 复制并修改消息
  ChatMessageModel copyWith({
    String? messageId,
    String? senderId,
    String? receiverId,
    ChatMessageType? messageType,
    String? content,
    DateTime? sendTime,
    bool? isRead,
  }) {
    return ChatMessageModel(
      messageId: messageId ?? this.messageId,
      senderId: senderId ?? this.senderId,
      receiverId: receiverId ?? this.receiverId,
      messageType: messageType ?? this.messageType,
      content: content ?? this.content,
      sendTime: sendTime ?? this.sendTime,
      isRead: isRead ?? this.isRead,
    );
  }

  /// 从JSON创建消息对象
  factory ChatMessageModel.fromJson(Map<String, dynamic> json) {
    return ChatMessageModel(
      messageId: json['messageId'] as String,
      senderId: json['senderId'] as String,
      receiverId: json['receiverId'] as String,
      messageType: ChatMessageType.values[json['messageType'] as int],
      content: json['content'] as String,
      sendTime: DateTime.parse(json['sendTime'] as String),
      isRead: json['isRead'] as bool? ?? false,
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'messageId': messageId,
      'senderId': senderId,
      'receiverId': receiverId,
      'messageType': messageType.index,
      'content': content,
      'sendTime': sendTime.toIso8601String(),
      'isRead': isRead,
    };
  }

  @override
  String toString() {
    return 'ChatMessageModel(messageId: $messageId, senderId: $senderId, receiverId: $receiverId, messageType: $messageType, content: $content, sendTime: $sendTime, isRead: $isRead)';
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is ChatMessageModel &&
        other.messageId == messageId &&
        other.senderId == senderId &&
        other.receiverId == receiverId &&
        other.messageType == messageType &&
        other.content == content &&
        other.sendTime == sendTime &&
        other.isRead == isRead;
  }

  @override
  int get hashCode {
    return messageId.hashCode ^
        senderId.hashCode ^
        receiverId.hashCode ^
        messageType.hashCode ^
        content.hashCode ^
        sendTime.hashCode ^
        isRead.hashCode;
  }
}
